package com.example.JD;

/**
 * Created by Quincy on 2018/9/15.
 * 找出一个字符串中字符连续相同的最长子串，如aabbccc，结果就是ccc
 */
public class GFG {

    public static void maxRepeating(String str){
        char[] array = str.toCharArray();
        int count = 1;
        int max = 0;
        char maxChar = 0;
        int maxIndex = 0;
        for (int i = 1; i < array.length; i++){
            if (array[i] == array[i-1]){
                count++;
            }else {
                if (count > max){
                    max = count;
                    maxChar = array[i-1];
                    maxIndex = i-1;
                }
                count = 1;
            }
        }
        //接下来这判断，是为了判断字符串最后的字符，因为超过超度跳出了上面的if判断
        if (count > max){
            max = count;
            maxChar = array[array.length - 1];
            maxIndex = array.length - 1;
        }
        System.out.println("Longest run: "+max+", for the character "+maxChar+"and the max char index is:"+maxIndex);
        System.out.println(str.substring(maxIndex-(max-1),maxIndex+1));
    }


    // Driver code
    public static void main(String args[])
    {
        String str = "ssstrtyweweqqqqqookkpppdddddddsaw";
        maxRepeating(str);
    }
}
